#include <iostream>
#include <string>
#include "Http.hpp"
#include "Tcpserver.hpp"

void Usage(std::string proc)
{
    std::cout << "Usage\n\t" << proc << " local_port\n"
              << std::endl;
}

std::shared_ptr<HttpResponse> Login(std::shared_ptr<HttpRequest> req)
{
    LOG(DEBUG, "=================================================\n");
    std::string userdata;
    if (req->Method() == "GET")
    {
        userdata = req->Args();
    }
    else if (req->Method() == "POST")
    {
        userdata = req->Text();
    }
    else
    {}

    // 1. 进程间通信，比如pipe！还有环境变量

    // 2. fork();

    // 3. exec();程序替换，执行其他语言代码：python/ php java / ……

    // 处理数据
    LOG(DEBUG, "enter data handle, data is : %s\n", userdata.c_str());



    auto response = Factory::BuildHttpResponse();
    response->AddStatusLine(200, "OK");
    response->AddHeander("Content-Type","text/html");
    response->AddText("<html><h1>handler data done</html><h1>");

    LOG(DEBUG, "=================================================\n");

    return response;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(1);
    }

    uint16_t port = std::stoi(argv[1]);
    HttpServer httpserver;
    httpserver.AddHeader("/login", Login);
    TcpServer server(port, std::bind(&HttpServer::HandlerHttpServer, &httpserver, std::placeholders::_1));
    server.Loop();
    return 0;
}